<?php
/**
 * 小程序添加账号
 */
namespace app\mini\controller;
class Account extends Pre
{
    //添加，修改
    public function inserts(){
        $account =new \app\common\model\Account();
        return $account -> inserts($this -> loginInfo);
    }

    //删除
    public function dels(){
        $account =new \app\common\model\Account();
        return $account -> dels();
    }

    /**
     * 传递账号列表信息
     * 默认传递次级账号，通过role搜索所有账号
     */
    public function lists(){
        $data = $this -> data;
        $loginInfo = $this -> loginInfo;
        if ($data['key'])
            $findMap[] = ['name|telephone','like',"%$data[key]%"];




        //获取所有经纪人，筛选职位
        if ($data['mark']){
//            $findMap[] = ['role','neq','33'];
            if ($data['mark'] !='all')
                $findMap[] = ['role','eq',$data['mark']];
            $allAcount = Account::allAcount($loginInfo['id']);
            $allAcount = array_diff($allAcount,[$loginInfo['id']]);
            $findMap[] = ['id','in',$allAcount];
        }
        else{
            //获取全民列表
            $findMap[] = $data['role']?['role','eq','33']:['role','neq','33'];
            $b_id = \app\common\model\Record::where('a_id','eq',$loginInfo['id'])->column('b_id');
            $findMap[] = ['id','in',$b_id];
        }

        $info = \app\common\model\Account::with('roleInfo,buildingInfo,distributeInfo,lower.BInfo')
        ->where($findMap)
        ->limit("$data[start],$data[count]")
        ->order('time desc')
        -> select()->toArray();

        $power = $loginInfo -> roleInfo-> power;
        $power = explode(',',$power);
        foreach ($info as $k => $value){
            $info[$k]['time'] = date('Y-m-d H:i:s',$value['time']);
            if (in_array($value['role'],$power))
                $info[$k]['my_mark'] = 1;

            $lowerNum = 0;
            //直接开出的账号数量。不包括全民
            foreach ($value['lower'] as $item){
//                if ($item['b_info']['role'] != '33')
                    $lowerNum += 1;
            }
            $info[$k]['lower_count'] = $lowerNum;

            //获取兑应所有的下级账号的数量,不包括全民
            $lowerAcount = Account::allAcount($value['id']);
            $lowerAcount = array_diff($lowerAcount,[$value['id']]);
            $allLower = \app\common\model\Account::where([
                ['id','in',$lowerAcount],
//                ['role','neq','33']
            ])->count();
            $info[$k]['all_lower_count'] = $allLower;
        }


        return [
            'content' => $info,
            'num' => \app\common\model\Account::where($findMap)->count(),
        ];
    }


    //传递我和我开的账号
    public static function allAcount($userID){
        $allAccount[] = $userID;
        $findRecord[] = ['a_id','eq',$userID];
        while (!empty($b_id = \app\common\model\Record::where($findRecord)->column('b_id'))){
            $findRecord = [];
            $allAccount = array_merge($allAccount,$b_id);
            $findRecord[] = ['a_id','in',$b_id];
        }
        return $allAccount;
    }

    //传递我能开的角色
    public function showRole()
    {
        $loginInfo = $this -> loginInfo;
        $info = \app\common\model\Role::where('id','eq',$loginInfo['role'])->find();
        $power = explode(',',$info['power']);
        $power = array_diff($power, ["33"]);
        $allPower = \app\common\model\Role::with('point')->where('id','in',$power)->select()->toArray();

        foreach ($allPower as $k=> $value){
            foreach ($value['point'] as $item){
                if ($item['building_id'] == $loginInfo['project']){
                    $allPower[$k]['point_info'] = explode(',',$item['point']);
                    break;
                }
            }
            if (!$allPower[$k]['point_info'])
                $allPower[$k]['point_info'] = [
                    '0',
                    '元'
                ];
        }
        foreach ($allPower as $k => $value){
            $allPower[$k]['word'] = explode(',',$value['word']);
        }

        $building = \app\common\model\Building::field('id,name')->select();

        $gongneng = \app\common\model\Gongneng::where('sign','eq','1')->select();
        //所有分销总
        $saler = \app\common\model\Account::where('role','eq','20')->field('id,name')->select();
        //所有的经纪人

        $findAgent[] = ['id','in',$this -> allAcount($loginInfo['id'])];
        $findAgent[] = ['role','not in',['5','6','17','18','15','4']];
        $allAgent = \app\common\model\Account::where('id','in',$this -> allAcount($loginInfo['id']))->select();
        return [
            'content' => $allPower,
            'building' => $building,
            'power' => $gongneng,
            'saler' => $saler,
            'allAgent' => $allAgent,
        ];
    }

    //个人中心
    public function center(){
        $info = $this -> loginInfo;
        $info -> roleInfo;
        $info -> buildingInfo;
        $info-> higher;
        $info['higherInfo'] = \app\common\model\Account::with('roleInfo')->where('id',$info['higher']['a_id'])->field('id,name,telephone,project,role')->find();

        //获取转介数量
        $cacheNum = cache('mini_num');

        $findTransfer[] = ['to_account_id','eq',$info['id']];
        $findTransfer[] = ['id','not in',$cacheNum['transfer']];
        $alltransfer = \app\common\model\Transfer::where($findTransfer)->count();
        $findWallet[] = ['account_id','eq',$info['id']];
        $findWallet[] = ['id','not in',$cacheNum['wallet']];
        $allWallet  = \app\common\model\Wallet::where($findWallet)->count();
        $info['num'] = [
            '4' => $alltransfer,
            '7' => $allWallet
        ];

        //获取未读公告
        $findInform[] = ['id','not in',cache('readNum'.$info['id'])];
        $findInform[] = ['date','elt',time()];
        $readNum = \app\common\model\Inform::where($findInform)->order('date desc')->select();
        foreach ($readNum as $k => $value){
            $readNum[$k]['date'] = date('Y-m-d H:i:s',$value['date']);
        }
        $info['inform'] = $readNum;
        return $info;
    }

    //账号信息
    public function detail(){
        $id = input('id');
        $info = \app\common\model\Account::where('id',$id)->with('buildingInfo,roleInfo')->find();
        if ($info)
            $info['time'] = date('Y-m-d H:i:s',$info['time']);
        return $info;
    }

    //上传头像
    public function photo(){
        //上传文件
        $URL = "./static/upload/photo/";
        $upload = new \app\common\logic\Upload();
        $pathName = $upload -> img('file',$URL);
        $loginInfo = $this -> loginInfo;
        \app\common\model\Account::where('id',$loginInfo['id'])
            ->update(['image' => ltrim($pathName[0],'.')]);
        return [
            'code' => '1',
            'message' => '上传成功'
        ];
    }

    //开全民经纪人 发送验证短信
    public function send(){
        $telephone = input('param.telephone');
        $send = new \app\common\logic\Send();
        return $send -> code($telephone,mt_rand(100000,999999),'wholeMan');
    }


}